import 'package:flutter_module/res/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import '../../res/image.dart';

/**
 * 加载状态
 */
enum LoadState { State_Success, State_Error, State_Loading, State_Empty }

/**
 * 空布局类型
 */
enum EmptyType {
  Empty_Normal,
  Empty_Search,
  Empty_histroyLikeSerch,
  Empty_Welfare_Ranking,
  Empty_Read,
  Empty_Habit,
  Empty_Like,
  Empty_Run,
  Empty_404,
  Empty_Network,
}

///根据不同状态来展示不同的视图
class LoadStateLayout extends StatefulWidget {
  final LoadState state; //页面状态
  final Widget successWidget; //成功视图
  final Widget? emptyWidget; //空数据视图
  final VoidCallback? errorRetry; //错误事件处理
  final VoidCallback? emptyRetry; //空数据事件处理(暂时没用到)
  final EmptyType type; //定义类型 用来显示不同的布局

  const LoadStateLayout(
      {Key? key,
      required this.state, //默认为加载状态
      required this.successWidget,
      this.emptyWidget,
      this.errorRetry,
      this.emptyRetry,
      this.type = EmptyType.Empty_Normal})
      : super(key: key);

  @override
  _LoadStateLayoutState createState() => _LoadStateLayoutState();
}

class _LoadStateLayoutState extends State<LoadStateLayout> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      //宽高都充满屏幕剩余空间
      width: double.infinity,
      height: double.infinity,
      child: _buildWidget,
    );
  }

  ///根据不同状态来显示不同的视图
  Widget get _buildWidget {
    switch (widget.state) {
      case LoadState.State_Success:
        return widget.successWidget;
      case LoadState.State_Error:
        return _errorView(widget.type, widget.errorRetry);
      case LoadState.State_Loading:
        return _loadingView;
      case LoadState.State_Empty:
        return widget.emptyWidget ?? NoDataView(widget.emptyRetry, widget.type);
      default:
        return _buildText("未知状态");
    }
  }

  ///加载中视图
  Widget get _loadingView {
    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[_buildText('加载中...')],
      ),
    );
  }

  ///错误视图
  Widget _errorView(EmptyType type, VoidCallback? errorRetry) {
    return SizedBox(
        child: Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Stack(
            alignment: Alignment.bottomCenter,
            children: [
              _buildImageByType(EmptyType.Empty_Network),
              _buildTextByType(EmptyType.Empty_Network)
            ],
          ),
          Padding(
            padding: const EdgeInsets.only(top: 37),
            child: _buildErrorBtn(errorRetry),
          )
        ],
      ),
    ));
  }
}

class NoDataView extends StatefulWidget {
  final VoidCallback? emptyRetry; //无数据事件处理
  final EmptyType type;

  const NoDataView(this.emptyRetry, this.type, {super.key});

  @override
  _NoDataViewState createState() => _NoDataViewState();
}

class _NoDataViewState extends State<NoDataView> {
  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.white,
        width: double.infinity,
        height: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              child: _buildImageAndText(widget.type),
            ),
          ],
        ));
  }
}

Widget _buildImageAndText(EmptyType type) {
  return Column(
    children: [_buildImageByType(type), _buildTextByType(type)],
  );
}

Widget _buildTextByType(EmptyType type) {
  String text;
  switch (type) {
    case EmptyType.Empty_Read:
      text = "暂无读书记录";
      break;
    case EmptyType.Empty_Search:
      text = "暂无相关搜索";
      break;
    case EmptyType.Empty_histroyLikeSerch:
      text = "暂时还没有点赞数据哦，快去给他人点赞吧～";
      break;
    case EmptyType.Empty_Network:
      text = "无法连接到网络";
      break;
    case EmptyType.Empty_Welfare_Ranking: //图片自带了数据为空的文本提示
      text = "";
      break;
    case EmptyType.Empty_Normal:
      text = "暂无";
      break;
    case EmptyType.Empty_Habit:
      text = "暂无习惯";
      break;
    case EmptyType.Empty_Run:
      text = "暂无跑步记录";
      break;
    case EmptyType.Empty_Like:
      text = "暂无点赞";
      break;
    case EmptyType.Empty_404:
      text = "当前页面不存在";
      break;
    default:
      text = "当前页面不存在";
      break;
  }

  return _buildText(text);
}

Widget _buildImageByType(EmptyType type) {
  String assetName;

  switch (type) {
    case EmptyType.Empty_Read:
      assetName = YouthImages.icEmptyRead;
      break;
    case EmptyType.Empty_Search:
      assetName = YouthImages.icEmptySearch;
      break;
    case EmptyType.Empty_histroyLikeSerch:
      assetName = YouthImages.icEmptyLike;
      break;
    case EmptyType.Empty_Network:
      assetName = YouthImages.icEmptyNetwork;
      break;
    case EmptyType.Empty_Normal:
      assetName = YouthImages.icEmptyNormal;
      break;
    case EmptyType.Empty_Habit:
      assetName = YouthImages.icEmptyHabit;
      break;
    case EmptyType.Empty_Run:
      assetName = YouthImages.icEmptyRun;
      break;
    case EmptyType.Empty_Welfare_Ranking:
      assetName = YouthImages.icWelfareRankingEmpty;
      break;

    case EmptyType.Empty_Like:
      assetName = YouthImages.icEmptyLike;
      break;
    case EmptyType.Empty_404:
      assetName = YouthImages.icEmpty404;
      break;
    default:
      assetName = YouthImages.icEmpty404;
      break;
  }
  return SvgPicture.asset(assetName);
}

Widget _buildErrorBtn(VoidCallback? onPressed) {
  return MaterialButton(
      onPressed: onPressed,

      ///按钮的阴影
      elevation: 0,

      ///按钮按下时的阴影高度
      highlightElevation: 0,
      color: YouthColors.color_F65C15,
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(20))),
      child: _buildText("请重试", color: Colors.white));
}

Widget _buildText(String text, {Color? color = YouthColors.color_969699}) {
  return Text(
    text,
    style: TextStyle(color: color, fontSize: 14),
  );
}
